package com.yang.core.plugins.monitoring;

import com.yang.core.model.ExceptionContext;
import com.yang.core.model.RequestContext;
import com.yang.core.model.ResponseContext;
import com.yang.core.api.RestClientInterceptor;
import com.yang.core.config.RestClientProperties;
import lombok.extern.slf4j.Slf4j;

/**
 * 监控日志拦截器
 */
@Slf4j
public class MonitoringLoggingInterceptor implements RestClientInterceptor {

    private final RestClientProperties.Monitoring monitoringConfig;

    public MonitoringLoggingInterceptor(RestClientProperties.Monitoring monitoringConfig) {
        this.monitoringConfig = monitoringConfig;
    }

    @Override
    public void preRequest(RequestContext context) {
        if (monitoringConfig.isLogRequests()) {
            log.info("发送HTTP请求: {} {} - Headers: {}",
                    context.getMethod(),
                    context.getUrl(),
                    context.getHeaders());
        }
    }

    @Override
    public void postResponse(ResponseContext context) {
        if (monitoringConfig.isLogResponses()) {
            log.info("收到HTTP响应: Status: {}, Duration: {}ms",
                    context.getStatusCode(),
                    "N/A"); // ResponseContext中没有getDuration方法
        }
    }

    @Override
    public void onError(ExceptionContext context) {
        log.error("HTTP请求异常: {} {} - Error: {}",
                context.getMethod(),
                context.getUrl(),
                context.getException().getMessage(),
                context.getException());
    }
}